<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item prop="vehicleId">
        <el-select v-model="queryParams.vehicleId" placeholder="请选择出库餐车" clearable>
          <el-option v-for="item in vehicles" :key="item.id" :label="item.vehicleName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="isGoodsOut" :label="text+'到车辆'" prop="vehicleId">
        <el-select v-model="form.vehicleId" :placeholder="`请选择餐车`" clearable :disabled="isUpdateMode" @change="onVehicleChange">
          <el-option v-for="item in vehicles" :key="item.id" :label="item.vehicleName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="storageId">
        <el-select v-model="queryParams.storageId" placeholder="请选择出库仓库" clearable>
          <el-option v-for="item in storages" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="outStockOrder">
        <el-input v-model="queryParams.outStockOrder" placeholder="请输入出库单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item prop="states">
        <el-select v-model="queryParams.states" placeholder="请选择出库状态" clearable>
          <el-option v-for="dict in dict.type.out_storage_status" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <!--<el-form-item label="出库人" prop="ingoingPerson">
        <el-input v-model="queryParams.ingoingPerson" placeholder="请输入出库人" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>-->
      <el-form-item prop="outgoingTime">
        <el-date-picker v-model="queryParams.outgoingTime" clearable type="date" value-format="yyyy-MM-dd" placeholder="请选择出库时间"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">创建</el-button>
        <!--<el-button type="primary" icon="el-icon-plus" size="mini" v-hasPermi="['storage:goodsOutStorage:add']" @click="handleAdd">创建</el-button>-->
        <!--<el-button type="warning" icon="el-icon-download" size="mini" v-hasPermi="['storage:goodsInStorage:export']" @click="handleExport">导出</el-button>-->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="goodsOutStorageList">
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="{$index}">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </el-table-column>
      <!--<el-table-column label="餐车ID" align="left" header-align="center" prop="vehicleId" />-->
      <el-table-column label="餐车名称" align="left" header-align="center" prop="vehicleName" />
      <!--<el-table-column label="仓库ID" align="left" header-align="center" prop="storageId" />-->
      <el-table-column label="仓库名称" align="left" header-align="center" prop="storageName" />
      <el-table-column label="出库单号" align="center" prop="outStockOrder" />
      <el-table-column label="出库状态" align="center" prop="states">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.out_storage_status" :value="scope.row.states" />
        </template>
      </el-table-column>
      <el-table-column label="出库人" align="center" prop="outgoingPerson" />
      <el-table-column label="出库时间" align="center" prop="outgoingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.outgoingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="修改人" align="center" prop="updateBy" />
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding" width="300">
        <template slot-scope="scope">
          <el-button v-hasPermi="['storage:goodsOutStorage:query']" size="mini" type="primary" @click="handleView(scope.row)">详情</el-button>
          <template v-if="scope.row.states===0">
            <el-button v-hasPermi="['storage:goodsOutStorage:edit']" size="mini" type="primary" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button v-hasPermi="['storage:goodsOutStorage:edit']" size="mini" type="primary" @click="handleStorageGoodsIn(scope.row)">出库</el-button>
            <el-button v-hasPermi="['storage:goodsOutStorage:remove']" size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList"/>

    <!--添加或修改商品出库对话框-->
    <storage-goods-list :id="id" :title="title" :visible.sync="open" type="out" :mode="mode" @ok="onOk" />
  </div>
</template>

<script>

import { listAllVehicle } from '@/api/vehicle/vehicle'
import { listAllStorage } from '@/api/storage/storage'
import { listGoodsOutStorage, getGoodsOutStorage, delGoodsOutStorage, updateGoodsOutStorage } from '@/api/storage/goodsOutStorage'
import { StorageGoodsList } from '../components/StorageGoodsList.vue'

export default {
  name: 'GoodsOutStorage',
  dicts: ['out_storage_status'],
  components: {
    StorageGoodsList
  },
  data() {
    return {
      // 按钮loading
      // buttonLoading: false,
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品出库表格数据
      goodsOutStorageList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vehicleId: undefined,
        storageId: undefined,
        outStockOrder: undefined,
        states: undefined,
        outgoingPerson: undefined,
        outgoingTime: undefined,
        remarks: undefined
      },
      vehicles: [],
      storages: [],
      mode: null,
      id: '',
      // 表单参数
      form: {},
      // 表单校验
      rules: {
         id: [
           { required: true, message: '出库ID不能为空', trigger: 'blur' }
         ],
         vehicleId: [
           { required: true, message: '餐车ID不能为空', trigger: 'blur' }
         ],
         storageId: [
           { required: true, message: '仓库ID不能为空', trigger: 'blur' }
         ],
         outStockOrder: [
           { required: true, message: '出库单号不能为空', trigger: 'blur' }
         ],
         states: [
           { required: true, message: '0:待出库;1:已出库；不能为空', trigger: 'blur' }
         ],
         outgoingPerson: [
           { required: true, message: '出库人不能为空', trigger: 'blur' }
         ],
         outgoingTime: [
           { required: true, message: '出库时间不能为空', trigger: 'blur' }
         ]
       }
    }
  },
  created() {
    this.getList()
    this.listAllVehicle()
    this.listAllStorage()
  },
  methods: {
    /** 查询商品出库列表 */
    getList() {
      this.loading = true
      listGoodsOutStorage(this.queryParams).then(response => {
        this.goodsOutStorageList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    listAllVehicle() {
      listAllVehicle().then(({ data }) => {
        this.vehicles = data
      })
    },
    listAllStorage() {
      listAllStorage().then(({ data }) => {
        this.storages = data
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        vehicleId: undefined,
        storageId: undefined,
        outStockOrder: undefined,
        states: undefined,
        outgoingPerson: undefined,
        outgoingTime: undefined,
        remarks: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      }
      this.resetForm('form')
      this.id = ''
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    async handleAdd() {
      this.mode = 'add'
      this.reset()
      this.open = true
      this.title = '创建出库单'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.mode = 'update'
      this.loading = true
      this.reset()
      const id = row.id
      this.open = true
      this.title = '编辑出库单'
      getGoodsOutStorage(id).then(response => {
        this.loading = false
        this.form = response.data
        this.open = true
        this.title = '修改商品出库'
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除？').then(() => {
        this.loading = true
        return delGoodsOutStorage(id)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      }).finally(() => {
        this.loading = false
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('storage/goodsOutStorage/export', {
        ...this.queryParams
      }, `goodsOutStorage_${new Date().getTime()}.xlsx`)
    },
    onOk() {
      if (this.mode !== 'view') {
        this.getList()
      }
    },
    handleStorageGoodsIn(row) {
      this.$modal.confirm('确定出库吗？').then(() => {
        this.loading = true
        return updateGoodsOutStorage({ ...row })
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('出库成功')
      }).catch(() => {
      }).finally(() => {
        this.loading = false
      })
    },
    handleView(row) {
      this.mode = 'view'
      this.reset()
      this.id = row.id
      this.open = true
      this.title = '出库单详情'
    }
  }
}
</script>
